package io.mft.runtime.function.initor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.mft.api.domain.Api;
import io.mft.api.domain.ApiFunctionType;
import io.mft.api.domain.ApiRegulation;
import io.mft.commons.exception.MFTException;
import io.mft.dev.base.ApiThreadExecutorConfig;
import io.mft.runtime.MFTApiExecutor;
import io.mft.runtime.cache.ApiCache;
import io.mft.runtime.function.ApiExecutorObject;
import io.mft.runtime.function.ApiFunctionInitor;
import io.mft.runtime.function.FunctionExecutor;
import io.mft.runtime.function.combination.CombinationFunctionExecutor;
import io.mft.runtime.function.combination.CombinationRegulationApiFunctionExecutor;
import lombok.extern.slf4j.Slf4j;
/**
 * 编排功能API初始化器
 * @说明：TODO
 * @类型名称：CombinationApiFunctionInitor
 * @创建者: 胡常建  
 * @创建时间: 2021年2月21日 下午5:44:47
 * @修改者: 胡常建  
 * @修改时间: 2021年2月21日 下午5:44:47
 */
@Slf4j
public class CombinationApiFunctionExecutorInitor extends ApiFunctionInitor {

	public CombinationApiFunctionExecutorInitor(ApiCache apiCache) {
		super(apiCache);
	}
	@Override
	protected ApiExecutorObject initApiExecutorObject(Api api) {
		Map<String, ApiThreadExecutorConfig> apiThreadExecutorConfigMap=new HashMap<>();
		List<ApiThreadExecutorConfig> apiThreadExecutorConfigs=MFTApiExecutor.getInstance().getRuntimeConfig().getApiThreadExecutorConfigs();
		apiThreadExecutorConfigs.forEach(e->{
			apiThreadExecutorConfigMap.put(e.threadExecutorId(), e);
		});
		
		FunctionExecutor functionExecutor=new CombinationFunctionExecutor();
		functionExecutor.setKey(api.getKey());
		List<FunctionExecutor> regulationExecutorList=new ArrayList<>();
		for (ApiRegulation ar : api.getApiRegulations()) {
			String[] values=ar.getRegulation().getValue().split(",");
			for (int i = 0; i < values.length; i++) {
				String v = values[i];
				ApiExecutorObject apiExecutorObject=getApiCache().getApiExecutorObject(v);
				if (apiExecutorObject==null) {
					throw new MFTException(-1,"未获取到接口["+v+"],请检查接口目录是否配置");
				}
				FunctionExecutor apiFunctionExecutor=apiExecutorObject.getFunctionExecutor();
				CombinationRegulationApiFunctionExecutor combinationRegulationApiFunctionExecutor=new CombinationRegulationApiFunctionExecutor(apiFunctionExecutor,ar);
				String key=api.getKey()+"."+ar.getPos()+"."+i+"."+v;
				combinationRegulationApiFunctionExecutor.setKey(key);
				String threadExecutorId=api.getModule().getSystem().getKey()+"."+api.getModule().getAliasKey()+"."+v;
				if (apiThreadExecutorConfigMap.containsKey(threadExecutorId)) {
					ar.getRegulation().setThreadExecutorId(threadExecutorId);
				}
				regulationExecutorList.add(combinationRegulationApiFunctionExecutor);
			}
		}
		FunctionExecutor[] regulationExecutors=new FunctionExecutor[regulationExecutorList.size()];
		regulationExecutorList.toArray(regulationExecutors);
		ApiExecutorObject apiExecutorObject=new ApiExecutorObject(api, functionExecutor, regulationExecutors);
		return apiExecutorObject;
	}

	@Override
	protected ApiFunctionType apiFunctionType() {
		return ApiFunctionType.combination;
	}
}
